package com.leoman.sensitivewords.service.impl;

import com.leoman.common.service.impl.GenericManagerImpl;
import com.leoman.sensitivewords.dao.SensitiveWordsDao;
import com.leoman.sensitivewords.entity.SensitiveWords;
import com.leoman.sensitivewords.service.SensitiveWordsService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by 蔡琦 on 2016/7/29 0029.
 *
 */
@Service
public class SensitiveWordsServiceImpl extends GenericManagerImpl<SensitiveWords, SensitiveWordsDao> implements SensitiveWordsService {

    @Autowired
    private SensitiveWordsDao sensitiveWordsDao;

    @Override
    public List<SensitiveWords> findByCode(String code) {
        return sensitiveWordsDao.findByCode(code);
    }

    @Override
    public String replaceWord(String content) {
        if (StringUtils.isEmpty(content)) {
            return content;
        }
        List<SensitiveWords> sensitiveWordsList = sensitiveWordsDao.findAll();
        for (SensitiveWords sensitiveWords : sensitiveWordsList) {

            content = content.replace(sensitiveWords.getWords(), "***");
        }

        return content;
        /*SensitiveWords sensitiveWords = sensitiveWordsDao.findByContent(content);
        if (null == sensitiveWords) {
            return content;
        } else {
            return content.replace(sensitiveWords.getWords(), "***");
        }*/
    }
}
